﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 人事工资管理系统
{
    public partial class frmEmployee : Form
    {
        public frmEmployee()
        {
            InitializeComponent();
        }
        PMSClass.DBOperate operate = new PMSClass.DBOperate();
        private void frmEmployee_Load(object sender, EventArgs e)
        {
            string str =
"select ID as '编码',employeeID as '员工编号',employeeName as '员工姓名',employeeSex as'员工性别',employeeDept as'所属部门',employeeBirthday as'员工生日',employeeNation as'民族',employeeMarriage as'婚姻状况',employeeDuty as'担任职务',employeePhone as'联系电话',employeeAccession as'就职日期'from tb_employee";
            operate.BindDataGridView(dgvEmployee, str);
            dgvEmployee.Columns[0].Width = 40;
            dgvEmployee.Columns[1].Width = 80;
            treeView1.ImageList = imageList1;
            treeView1.ImageIndex = 0;
            treeView1.SelectedImageIndex = 0;
            string sql = "select count(*) from tb_employee";
            toolStripStatusLabel2.Text =operate.HumanNum(sql).ToString() + "人";
            TreeNode tn = treeView1.Nodes.Add("所有部门");
            SqlConnection conn = PMSClass.DBConnection.MyConnection();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from tb_department",conn);
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                tn.Nodes.Add(sdr["DName"].ToString());
            }
            sdr.Close();
            conn.Close();
            treeView1.ExpandAll();

        }

        private void dgvEmlployee_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            toolStripLabel2_Click(sender, e);
        }


        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string str = e.Node.Text;
            if (str == "所有部门")
            {
                string SQLstr =
"select ID as '编码',employeeID as '员工编号',employeeName as '员工姓名',employeeSex as'员工性别',employeeDept as'所属部门',employeeBirthday as'员工生日',employeeNation as'民族',employeeMarriage as'婚姻状况',employeeDuty as'担任职务',employeePhone as'联系电话',employeeAccession as'就职日期'from tb_employee";
                operate.BindDataGridView(dgvEmployee, SQLstr);
            }
            else
            {
                string strsql =
    "select ID as '编码',employeeID as '员工编号',employeeName as '员工姓名',employeeSex as'员工性别',employeeDept as'所属部门',employeeBirthday as'员工生日',employeeNation as'民族',employeeMarriage as'婚姻状况',employeeDuty as'担任职务',employeePhone as'联系电话',employeeAccession as'就职日期'from tb_employee where employeeDept='"+str+"'";
                operate.BindDataGridView(dgvEmployee, strsql);
                dgvEmployee.Columns[0].Width = 40;
                dgvEmployee.Columns[1].Width = 80;

            }
        }

        private void dgvEmployee_Click(object sender, EventArgs e)
        {
            if (dgvEmployee.SelectedCells.Count > 0)
            {
                string YGName = dgvEmployee.SelectedCells[1].Value.ToString();
                operate.Get_Image(YGName, pictureBox1);
            }
        }


        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            frmAddEmployee Addemployee = new frmAddEmployee();
            Addemployee.ShowDialog();
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
         
            if (dgvEmployee.SelectedCells.Count == -1)
            {
                MessageBox.Show("请选择要修改的数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                string YGName = dgvEmployee.SelectedCells[2].Value.ToString();
                frmEmployeeInfo info = new frmEmployeeInfo();
                info.YGName = YGName;
                info.YGID = dgvEmployee.SelectedCells[1].Value.ToString();
                info.ShowDialog();
            }

        }

        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            try
            {
                string YGID = dgvEmployee.SelectedCells[1].Value.ToString();
                string DelSql = "delete from tb_employee where employeeID='" + YGID + "'";
                operate.OperateData(DelSql);
                MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();

            }
            catch
            {
                MessageBox.Show("删除操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void toolStripTextBox2_TextChanged(object sender, EventArgs e)
        {
            string str =
 "select ID as '编码',employeeID as '员工编号',employeeName as '员工姓名',employeeSex as'员工性别',employeeDept as'所属部门',employeeBirthday as'员工生日',employeeNation as'民族',employeeMarriage as'婚姻状况',employeeDuty as'担任职务',employeePhone as'联系电话',employeeAccession as'就职日期'from tb_employee where employeeName like '%" + toolStripTextBox2.Text.Trim() + "%'";
            operate.BindDataGridView(dgvEmployee, str);
            dgvEmployee.Columns[0].Width = 40;
            dgvEmployee.Columns[1].Width = 80;
        }
    }
}
